package com.yanqu.road.server.cmd.cross.activity;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.UnionActivityConditionData;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.entity.enums.eCrossUnionRankActivityType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.logic.helper.UnionActivityGroupHelper;
import com.yanqu.road.pb.rank.YRankProto;
import com.yanqu.road.server.manger.union.GameUnionActivityGroupMgr;
import com.yanqu.road.server.protocol.CrossProtocol;
import com.yanqu.road.utils.YanQuMessageUtils;
import com.yanqu.road.pb.activity.ActivityProto;
import com.yanqu.road.pb.crossunion.activity.CrossUnionActivityRankDataProto;
import com.yanqu.road.pb.rank.CrossUnionActivityRankProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.manger.activity.NormalActivityMgr;
import com.yanqu.road.server.manger.player.GamePlayerMgr;
import com.yanqu.road.server.manger.union.UnionActivityMgr;
import com.yanqu.road.server.pb.ActivityPb;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.Protocol;

@Cmd(code = Protocol.S_CROSS_ACTIVITY_UNION_MEMBER_CONDITION_DATA, desc = "跨服商会争霸成员贡献")
public class CrossActivityUnionMemberConditionDataCmd extends AbstractCommand {

	private int activityId;

	private String unionUid;

	private long serverId;

	private UnionActivityConditionData unionActivityConditionData;

	private boolean needResp = true;

	private CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataReqMsg  reqMsg;

	private ActivityInfo activityInfo;

	@Override
	public boolean parseData(YanQuMessage packet) throws Exception {
		reqMsg = CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataReqMsg.parseFrom(packet.getBytes());
		activityId = reqMsg.getActivityId();
		unionUid = reqMsg.getUnionUid();
		serverId = reqMsg.getServerId();
		return true;
	}

	@Override
	public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
		activityInfo = NormalActivityMgr.getActivityInfo(activityId);
		if(null == activityInfo){
			ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
			return;
		}
		if(false) {
			unionActivityConditionData = UnionActivityMgr.getUnionActivityConditionData(activityId, unionUid);
			if (null == unionActivityConditionData) {
				ret = GameErrorCode.E_ACTIVITY_NO_JOIN;
				return;
			}
		}else {//到跨服查数据
			needResp = false;

			// 商会分组活动走这边
			if (UnionActivityGroupHelper.isGroupActivityType(activityInfo.getType())) {
				YRankProto.YRankReqMsg.Builder builder = YRankProto.YRankReqMsg.newBuilder();
				builder.setActivityId(activityId);
				builder.setGroupId(GameUnionActivityGroupMgr.getGroupId(activityId, player.getUserId()));
				builder.setIdKey(String.valueOf(eGamePlayerEventType.UnionExpRiseRank.getValue()));
				builder.setSeeUnionUid(unionUid);
				player.sendUnionActivityGroupPacket(CrossProtocol.C2_CROSS_YRANK_GET_UNION_RANK_CONTRIBUTION, builder, activityId);
				return;
			}

			CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.Builder msg = CrossUnionActivityRankDataProto.CrossUnionMemberValueReqMsg.newBuilder();
			msg.setActivityId(activityId);
			msg.setUnionUid(unionUid);
			msg.setServerId(serverId);
			msg.setType(0);
			msg.setRankType(1);
			YanQuMessage pbMsg = YanQuMessageUtils.buildMessage(Protocol.C_CROSS_UNION_ACTIVITY_MEMBER_VALUE, msg);
			GamePlayerMgr.sendPacket(player.getUserId(), pbMsg);
		}
	}

	@Override
	public void sendData(GamePlayer player) {
		if(needResp) {
			CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.Builder respMsg = CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.newBuilder();
			respMsg.setRet(ret);
			if (0 == ret) {
				if (null != unionActivityConditionData) {
					ActivityProto.UnionRankMemberConditionDataListMsg.Builder dataMsg = ActivityPb.parseUnionRankMemberConditionDataListMsg(
							UnionActivityMgr.getUserActivityConditionDataList(unionActivityConditionData, UnionActivityMgr.getUserActivityConditionDataMap(activityId)));
					respMsg.setDataList(dataMsg);
					if (activityInfo.getType() == eActivityType.CrossUnionActivity.getValue()
							&& activityInfo.getChildType() == eCrossUnionRankActivityType.unionExpRiseRank.getIntValue()) {
						calcUnionTradeExpAdd(unionActivityConditionData.getValue().intValue(), respMsg);
					}
				}
			}
			player.sendPacket(Protocol.U_CROSS_ACTIVITY_UNION_MEMBER_CONDITION_DATA, respMsg);
		}
	}

	public void calcUnionTradeExpAdd(int unionExpAdd, CrossUnionActivityRankProto.CrossUnionActivityConditionUnionMemberDataRespMsg.Builder respMsg) {
		int unionTradeExpAdd;
		int unionMemberExpAdd = 0;
		ActivityProto.UnionRankMemberConditionDataListMsg dataList = respMsg.getDataList();
		for (ActivityProto.UnionRankMemberConditionDataTempMsg dataTempMsg : dataList.getDataListList()) {
			unionMemberExpAdd += Integer.parseInt(dataTempMsg.getValue());
		}
		unionTradeExpAdd = unionExpAdd - unionMemberExpAdd;
		if (unionTradeExpAdd < 0) {
			unionTradeExpAdd = 0;
		}
		respMsg.setParam(unionTradeExpAdd);
	}
}
